// V-webapp/src/api/cart.ts
import request from './request'

// 购物车项接口
export interface CartItem {
  id: number
  cart_id?: number
  product_id: number
  quantity: number
  product: {
    id: number
    product_id?: number
    name: string
    product_name?: string
    description: string
    price: number
    stock: number
    stock_quantity?: number
    cover_image?: string
    product_images?: Array<{
      image_url: string
    }>
    anchor?: {
      anchor_id: number
      anchor_name: string
    }
  }
}

// 购物车响应接口
export interface CartResponse {
  items: CartItem[]
  summary: {
    total: string
    totalQuantity: number
    itemCount: number
  }
}

// 获取购物车列表
export const getCart = () => {
  return request.get<CartResponse>('/cart')
}

// 获取购物车数量
export const getCartCount = () => {
  return request.get<{ count: number }>('/cart/count')
}

// 添加商品到购物车 - 修正路径
export const addToCart = (data: {
  product_id: number
  quantity: number
}) => {
  return request.post('/cart/add', data)  // 正确路径：/cart/add
}

// 更新购物车商品数量 - 修正路径
export const updateCartItem = (cartId: number, data: {
  quantity: number
}) => {
  return request.put(`/cart/${cartId}`, data)  // 正确路径：/cart/{cart_id}
}

// 移除购物车商品 - 修正路径
export const removeCartItem = (cartId: number) => {
  return request.delete(`/cart/${cartId}`)  // 正确路径：/cart/{cart_id}
}

// 清空购物车 - 修正路径
export const clearCart = () => {
  return request.delete('/cart')  // 正确路径：/cart
}